﻿#region Usings
using System.Collections.Generic;
using System.ComponentModel;
using System.Diagnostics;
using System.Linq;
using System.Web.Mvc;
using BK.Model;
using BK.Model.ViewModels;
using BK.Repositories.Abstract;
#endregion

namespace BK.Web.UI.Controllers
{
    [Authorize]
    public class PaymentsController : BaseController<Payment>
    {
        #region Readonly & Static Fields
        private readonly IAccountsRepository _accountsRepository;
        private readonly ITargetsRepository _targetsRepository;
        private readonly ICategoriesRepository _categoriesRepository;
        private readonly IUsersRepository _usersRepository;
        #endregion

        #region C'tors
        public PaymentsController(IPaymentsRepository entitiesRepository, IUsersRepository usersRepository,
                                  ICategoriesRepository categoriesRepository, IAccountsRepository accountsRepository,
                                  ITargetsRepository targetsRepository)
            : base(entitiesRepository, usersRepository)
        {
            _usersRepository = usersRepository;
            _categoriesRepository = categoriesRepository;
            _accountsRepository = accountsRepository;
            _targetsRepository = targetsRepository;
        }
        #endregion

        #region Instance Methods
        public ViewResult Create()
        {
            return View("Edit", CreateEditViewModel(new Payment{UserId = LoggedInUser.Id}));
        }

        [HttpPost]
        public ActionResult Create(Payment payment)
        {
            return Edit(payment);
        }

        [HttpPost]
        public ActionResult Edit(Payment payment)
        {
            if (ModelState.IsValid)
            {
                EntitiesRepository.Save(payment);
                return RedirectToAction("Index");
            }

            return View("Edit", CreateEditViewModel(payment));
        }

        public ActionResult Edit(int id)
        {
            return View("Edit", CreateEditViewModel(EntitiesRepository.GetById(id)));
        }

        public ViewResult Index([DefaultValue(1)] int page)
        {
            PaymentListViewModel model = new PaymentListViewModel();

            IEnumerable<Payment> allEntities = EntitiesRepository.GetAll().OrderByDescending(p => p.DateTime);
            allEntities = allEntities.Where(e => e.UserId == LoggedInUser.Id);

            model.PagingInfo.TotalItems = allEntities.Count();

            model.Payments = allEntities.Skip((page - 1) * PageSize).Take(PageSize).ToList();
            model.UserSelectionViewModel.Users = _usersRepository.GetAll().ToList();
            model.UserSelectionViewModel.UserId = LoggedInUser.Id;

            model.PagingInfo.CurrentPage = page;
            model.PagingInfo.ItemsPerPage = PageSize;

            return View("Index", model);
        }

        private PaymentEditViewModel CreateEditViewModel(Payment payment)
        {
            Debug.Assert(LoggedInUser.Id != 0, "LoggedInUser.Id != null");
            return new PaymentEditViewModel
                       {
                           Payment = payment,
                           Categories = _categoriesRepository.GetBy(LoggedInUser.Id).ToList(),
                           Accounts = _accountsRepository.GetBy(LoggedInUser.Id).ToList(),
                           Targets = _targetsRepository.GetBy(LoggedInUser.Id).ToList(),
                       };
        }
        #endregion
    }
}